#!/usr/bin/python
# -*- coding:utf-8 -*-
__author__ = 'good'
__createday__ = '2014-11-28'

from golsee.models import Blog_info, Settings, User, Role
from datetime import datetime
from flask.ext.script import Manager
from golsee import db
from config import BASEDIR

InitManager = Manager(usage=u'初始化博客')


@InitManager.command
def data():
    '初始化数据'
    if Settings.query.filter_by(type='init_date').first():
        print '已初始化数据，不能重复初始化'
    else:
        admin_user()
        Setting_data()
        Blog_info_data()


@InitManager.command
def tables():
    '初始化数据库结构'
    db.create_all()
    print '初始化数据库结构...[确定]'


@InitManager.command
def evn():
    '初始化运行环境'
    from subprocess import Popen

    Popen('pip install --upgrade -r ' + BASEDIR + '/required.txt', shell=True).wait()
    print '初始化运行环境...[确定]'


@InitManager.command
def stat():
    '初始化状态检测'
    try:
        date = Settings.query.filter_by(type='init_date').first()
        if not date:
            print '未初始化表数据'
        else:
            print 'golsee 已初始化'
    except Exception, e:
        if str(e).find('OperationalError') >= 0:
            print '数据连接错误，请检查配置'
        elif str(e).find('ProgrammingError') >= 0:
            print '表不存在，请初始化数据库结构'
        else:
            print e
            print '未知错误'


@InitManager.command
def all():
    '初始化所有'
    print '第一步：初始化运行环境...'
    evn()
    print '第二步：初始化数据库...'
    tables()
    print '第三步：初始化基本数据...'
    data()


def Blog_info_data():
    blog_info = Blog_info()
    blog_info.date = str(datetime.now().date())
    blog_info.visit_all = 0
    blog_info.visit_day = 0
    blog_info.visit_month = 0
    blog_info.visit_attack = 0
    blog_info.visit_attack_day = 0
    blog_info.visit_robot = 0
    blog_info.visit_robot_day = 0
    blog_info.article_all = 0
    blog_info.article_month = 0
    blog_info.user_all = 0
    blog_info.login_all = 0
    db.session.add(blog_info)
    db.session.commit()


def Setting_data():
    # 其他
    default = [('本站用户', ' admin.users', ' admin_second_bar ', 1, 4, 'glyphiconglyphicon-user'),
               ('文章分类', 'admin.category', 'admin_second_bar', 1, 2, 'glyphiconglyphicon-leaf'),
               ('本站文章', 'admin.article', 'admin_second_bar', 1, 3, 'glyphiconglyphicon-book'),
               ('本站概况', 'admin.index', 'admin_second_bar', 1, 1, 'glyphiconglyphicon-home'),
               ('这个菜单', 'admin.admin_second_bar', 'admin_second_bar', 1, 9, 'glyphiconglyphicon-tree-conifer'),
               ('博客名称', 'Golsee', 'blog_name', 1, '', ''),
               ('基本设置', 'admin.settings', 'admin_second_bar', 1, 7, 'glyphiconglyphicon-wrench'),
               ('主页', 'main.index', 'first_bar', 1, 1, 'glyphiconglyphicon-home'),
               ('留言', 'main.msg', 'first_bar', 1, 2, 'glyphiconglyphicon-comment'),
               ('关于', 'main.about', 'first_bar', 1, 4, 'glyphiconglyphicon-info-sign'),
               ('图片管理', 'admin.imgs', 'admin_second_bar', 1, 5, 'glyphiconglyphicon-picture'),
               ('附件管理', 'admin.atts', 'admin_second_bar', 1, 6, 'glyphiconglyphicon-file'),
               ('我的日历', 'admin.calendar', 'admin_second_bar', 0, 8, 'glyphiconglyphicon-calendar'),
               ('日历', 'main.calendar', 'first_bar', 1, 3, 'glyphiconglyphicon-calendar'),
               ('本站备份', 'admin.backup', 'admin_second_bar', 1, 10, 'glyphicon glyphicon-hdd'),
               ('默认主题', 'color', 'DEFAULT_THEME', 1, '', ''),
               ('邮件服务器地址', '', 'MAIL_SERVER', 1, '', ''),
               ('邮件服务器端口', '465', 'MAIL_PORT', 1, '', ''),
               ('是否使用TLS', 'False', 'MAIL_USE_TLS', 1, '', ''),
               ('是否使用SSL', 'True', 'MAIL_USE_SSL', 1, '', ''),
               ('邮件服务器用户名', '', 'MAIL_USERNAME', 1, '', ''),
               ('邮件服务器密码', '', 'MAIL_PASSWORD', 1, '', ''),
               ('管理员邮箱', '', 'ADMIN', 1, '', ''), ]
    for data in default:
        setting = Settings()
        setting.name, setting.value, setting.type, setting.is_use, setting.seq, setting.icon = data
        db.session.add(setting)
    # blog名称
    blog_name = raw_input('输入blog名称：')
    setname = Settings()
    setname.value = blog_name
    setname.type = 'blog_name'
    setname.is_use = 1
    db.session.add(setname)
    # 初始化时间
    settdate = Settings()
    settdate.value = str(datetime.now().date())
    settdate.type = 'init_date'
    settdate.is_use = 1
    db.session.add(settdate)
    db.session.commit()


def admin_user():
    email = raw_input('输入管理员邮箱：')
    inp = 1
    while inp == 1:
        pwd1 = raw_input('输入管理员密码：')
        pwd2 = raw_input('确认管理员密码：')
        if pwd1 == pwd2:
            inp = 0
        else:
            print '两次输入的不一样，请重新输入'
            continue
    print '--------------------------------'
    print '输入的管理员邮箱为：%s' % email
    print '输入的管理员密码为：%s' % pwd1
    print '--------------------------------'
    print '设置role...[成功]'
    Role.insert_roles()
    admin_id = Role.query.filter_by(permissions=0xff).first().id
    print '--------------------------------'
    user = User.query.filter_by(email=email).first()
    if user:
        if raw_input('%s 已存在，是否更新(Y)' % email).lower() == 'y':
            user.register_date = datetime.now(),
            user.role = admin_id
            user.set_password(pwd1)
    elif raw_input('确定（Y）').lower() == 'y':
        user = User(email=email,
                    nicename=email)
        user.register_date = datetime.now(),
        user.role = admin_id
        user.set_password(pwd1)
    db.session.add(user)
    db.session.commit()
    print '保存设置...[确定]'